package com.samehope.ar.controller;


import com.samehope.ar.common.CommonResult;
import com.samehope.ar.dto.ProjectTeamAddParam;
import com.samehope.ar.dto.ProjectTeamUpdateParam;
import com.samehope.ar.service.IProjectTeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 项目团队 前端控制器
 * </p>
 *
 * @author Zhang Luo
 * @since 2019-12-23
 */
@RestController
@RequestMapping("/projectTeam")
public class ProjectTeamController {

    @Autowired
    private IProjectTeamService projectTeamService;

    /**
     * 添加团队
     * @param param
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @PostMapping("/add")
    public CommonResult add(@RequestBody @Valid ProjectTeamAddParam param){
        projectTeamService.add(param);
        return CommonResult.success(null);
    }

    /**
     * 更新团队信息
     * @param param
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @PutMapping("/update")
    public CommonResult update(@RequestBody @Valid ProjectTeamUpdateParam param){
        projectTeamService.update(param);
        return CommonResult.success(null);
    }

    /**
     * 删除项目团队
     * @param teamId
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @DeleteMapping("/del/{teamId}")
    public CommonResult del(@PathVariable("teamId") Long teamId){
        projectTeamService.del(teamId);
        return CommonResult.success(null);
    }

    /**
     * 获取团队成员树结构
     * @return
     */
    @PreAuthorize("hasAnyRole('PROJECT', 'PRO_DATA')")
    @GetMapping("/tree")
    public CommonResult tree() {
        return CommonResult.success(projectTeamService.getTree());
    }

    /**
     * 获取团队下拉框数据
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @GetMapping("/list")
    public CommonResult listForSelect() {
        return CommonResult.success(projectTeamService.listForSelect());
    }
}
